//
//  MMSocialManager.m
//  MMShare
//
//  Created by qianqichao on 2018/3/15.
//  Copyright © 2018年 qianqichao. All rights reserved.
//

#import "MMSocialManager.h"
#import <UShareUI/UMSocialUIManager.h>
#import <UMSocialCore/UMSocialCore.h>
#import "MMSocialResponse.h"

@implementation MMSocialManager

+ (instancetype)defaultManager {
    static MMSocialManager *instance = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

+ (BOOL)isInstallWeChat {
    return [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_WechatSession];
}

+ (BOOL)isInstallQQ {
    return [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_QQ];
}

+ (BOOL)isInstallWeibo {
    return [[UMSocialManager defaultManager] isInstall:UMSocialPlatformType_Sina];
}

- (void)initialConfigurationWithUMAppKey:(NSString *)umAppKey wechatAppKey:(NSString *)wechatAppKey wechatAppSecret:(NSString *)wechatAppSecret qqAppKey:(NSString *)qqAppKey qqAppSecret:(NSString *)qqAppSecret sinaAppKey:(NSString *)sinaAppKey sinaAppSecret:(NSString *)sinaAppSecret openLog:(BOOL)openLog {
    UMSocialManager * socialManager = UMSocialManager.defaultManager;
    socialManager.umSocialAppkey = umAppKey;
    [socialManager setPlaform:UMSocialPlatformType_WechatSession appKey:wechatAppKey appSecret:wechatAppSecret redirectURL:@"http://mobile.umeng.com/social"];
    [socialManager setPlaform:UMSocialPlatformType_QQ appKey:qqAppKey appSecret:([qqAppSecret isEqualToString:@""] ? nil : qqAppSecret) redirectURL:@"http://mobile.umeng.com/social"];
    [socialManager setPlaform:UMSocialPlatformType_Sina appKey:sinaAppKey appSecret:sinaAppSecret redirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    [socialManager openLog:openLog];
}


- (void)shareMenu:(NSArray *)platforms callBack:(MMSocialSharePlatformSelectionBlock)callBack {
    //设置面板样式
    [UMSocialShareUIConfig shareInstance].shareTitleViewConfig.isShow = NO;
    [UMSocialShareUIConfig shareInstance].shareCancelControlConfig.shareCancelControlText = @"取消";
    
    NSMutableArray *convertPlatforms = [[NSMutableArray alloc] initWithCapacity:20];
    for (int i = 0; i < platforms.count; i ++) {
        NSInteger platform = [platforms[i] integerValue];
        UMSocialPlatformType plat = UMSocialPlatformType_WechatSession;
        switch (platform) {
            case MMSharePlatform_Wechat:
                plat = UMSocialPlatformType_WechatSession;
                break;
            case MMSharePlatform_Timeline:
                plat = UMSocialPlatformType_WechatTimeLine;
                break;
            case MMSharePlatform_QQ:
                plat = UMSocialPlatformType_QQ;
                break;
            case MMSharePlatform_Sina:
                plat = UMSocialPlatformType_Sina;
                break;
            case MMSharePlatform_Qzone:
                plat = UMSocialPlatformType_Qzone;
                break;
            default:
                break;
        }
        [convertPlatforms addObject:@(plat)];
    }
    //预定义平台
    [UMSocialUIManager setPreDefinePlatforms:convertPlatforms];
    //显示分享面板
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        
        MMSharePlatform plat = MMSharePlatform_Wechat;
        switch (platformType) {
            case UMSocialPlatformType_WechatSession:
                plat = MMSharePlatform_Wechat;
                break;
            case UMSocialPlatformType_WechatTimeLine:
                plat = MMSharePlatform_Timeline;
                break;
            case UMSocialPlatformType_QQ:
                plat = MMSharePlatform_QQ;
                break;
            case UMSocialPlatformType_Sina:
                plat = MMSharePlatform_Sina;
                break;
            case UMSocialPlatformType_Qzone:
                plat = MMSharePlatform_Qzone;
                break;
            default:
                break;
        }
        callBack(plat, userInfo);
    }];
}

- (void)shareToPlatform:(MMSharePlatform)platform shareType:(MMShareType)shareType content:(NSString *)content link:(NSString *)link title:(NSString *)title data:(id)data viewController:(UIViewController *)viewController completion:(MMSocialRequestCompletionHandler)completion {
    UMSocialMessageObject * messageObject = [self shareType:shareType content:content link:link title:title data:data];
    UMSocialPlatformType plat = UMSocialPlatformType_WechatSession;
    switch (platform) {
        case MMSharePlatform_Wechat:
            plat = UMSocialPlatformType_WechatSession;
            break;
        case MMSharePlatform_Timeline:
            plat = UMSocialPlatformType_WechatTimeLine;
            break;
        case MMSharePlatform_QQ:
            plat = UMSocialPlatformType_QQ;
            break;
        case MMSharePlatform_Sina:
            plat = UMSocialPlatformType_Sina;
            break;
        case MMSharePlatform_Qzone:
            plat = UMSocialPlatformType_Qzone;
            break;
        default:
            break;
    }
    [[UMSocialManager defaultManager] shareToPlatform:plat messageObject:messageObject currentViewController:viewController completion:^(id response, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        } else{
            if ([response isKindOfClass:[UMSocialShareResponse class]]) {
                
                UMSocialShareResponse *resp = response;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                //分享成功调用
                
                UMSocialShareResponse * umResponse = (UMSocialShareResponse *)response;
                MMSocialShareResponse * mResponse = [[MMSocialShareResponse alloc] init];
                mResponse.message = umResponse.message;
                mResponse.uid = umResponse.uid;
                mResponse.openid = umResponse.openid;
                mResponse.refreshToken = umResponse.refreshToken;
                mResponse.expiration = umResponse.expiration;
                mResponse.accessToken = umResponse.accessToken;
                mResponse.unionId = umResponse.unionId;
                mResponse.usid = umResponse.usid;
                mResponse.originalResponse = umResponse.originalResponse;
                completion(mResponse, error);
                
            } else {
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}

- (BOOL)handleOpenURL:(NSURL *)url options:(NSDictionary *)options {
    return [[UMSocialManager defaultManager] handleOpenURL:url options:options];
}

- (void)getUserInfoWithPlatform:(MMSharePlatform)platform viewController:(UIViewController *)viewController completion:(MMSocialRequestUserInfoCompletionHandler)completion {
    UMSocialPlatformType plat = UMSocialPlatformType_WechatSession;
    switch (platform) {
        case MMSharePlatform_Wechat:
            plat = UMSocialPlatformType_WechatSession;
            break;
        case MMSharePlatform_Timeline:
            plat = UMSocialPlatformType_WechatTimeLine;
            break;
        case MMSharePlatform_QQ:
            plat = UMSocialPlatformType_QQ;
            break;
        case MMSharePlatform_Sina:
            plat = UMSocialPlatformType_Sina;
            break;
        case MMSharePlatform_Qzone:
            plat = UMSocialPlatformType_Qzone;
            break;
        default:
            break;
    }
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:plat currentViewController:viewController completion:^(id response, NSError *error) {
        if (error == nil) {
            _fetchUserError = nil;
            UMSocialUserInfoResponse * umResponse = (UMSocialUserInfoResponse *)response;
            MMSocialUserInfoResponse * mResponse = [[MMSocialUserInfoResponse alloc] init];
            mResponse.name = umResponse.name;
            mResponse.iconurl = umResponse.iconurl;
            mResponse.unionGender = umResponse.unionGender;
            mResponse.gender = umResponse.gender;
            mResponse.uid = umResponse.uid;
            mResponse.openid = umResponse.openid;
            mResponse.refreshToken = umResponse.refreshToken;
            mResponse.expiration = umResponse.expiration;
            mResponse.accessToken = umResponse.accessToken;
            mResponse.unionId = umResponse.unionId;
            mResponse.usid = umResponse.usid;
            mResponse.originalResponse = umResponse.originalResponse;
            completion(mResponse, _fetchUserError);
        } else {
            MMSocialUserInfoResponse * mResponse = [[MMSocialUserInfoResponse alloc] init];
            _fetchUserError = [[NSError alloc] initWithDomain:error.domain code:error.code userInfo:error.userInfo];
            completion(mResponse, _fetchUserError);
        }
        
    }];
}

- (UMSocialMessageObject *)shareType:(MMShareType)shareType content:(NSString *)content link:(NSString *)link title:(NSString *)title data:(id)data {
    UMShareWebpageObject * webObject = [[UMShareWebpageObject alloc] init];
    webObject.webpageUrl = link;
    webObject.descr = content;
    webObject.title = title;
    webObject.thumbImage = data;
    
    UMShareImageObject * imageObject = [[UMShareImageObject alloc] init];
    imageObject.shareImage = data;
    imageObject.title = title;
    imageObject.descr = content;
    
    UMSocialMessageObject * messageObject = [[UMSocialMessageObject alloc] init];
    switch (shareType) {
        case MMShareType_Web:
            messageObject.shareObject = webObject;
            break;
        case MMShareType_Image:
            messageObject.shareObject = imageObject;
            break;
        default:
            break;
    }
    return messageObject;
}

@end
